package com.crape.faq.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crape.account.bo.IAccountBO;
import com.crape.common.bean.LoginUser;
import com.crape.common.constants.MessageCode;
import com.crape.common.enums.QuestionStatusType;
import com.crape.common.enums.ResultType;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Answer;
import com.crape.common.model.Question;
import com.crape.common.thread.ThreadObjManager;
import com.crape.common.utils.PageUtils;
import com.crape.faq.bo.IAnswerBO;
import com.crape.faq.bo.IQuestionBO;

@Controller
public class QuestionController {

	private IQuestionBO questionBOImpl;
	private IAnswerBO answerBOImpl;
	private IAccountBO accountBOImpl;

	@Autowired
	public void setquestionBOImpl(IQuestionBO questionBOImpl) {
		this.questionBOImpl = questionBOImpl;
	}

	@Autowired
	public void setanswerBOImpl(IAnswerBO answerBOImpl) {
		this.answerBOImpl = answerBOImpl;
	}

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@RequestMapping("/faq/questions.jsp")
	public String questions(ModelMap modelMap, Integer pageNo) {
		Long counts = questionBOImpl.countPageQuestions();
		if (counts == null || counts == 0) {
			return "/faq/questions.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Integer pageSize = 20;
		Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
		List<Question> questions = questionBOImpl.getPagedQuestions(startIndex, pageSize);
		modelMap.put("questions", questions);
		modelMap.put("pageNo", pageNo);
		modelMap.put("pageNumArray", PageUtils.getPagedNumArray(counts, pageSize));
		List<AccountInfo> hotUsers = accountBOImpl.getPagedAccountByFaq(16);
		modelMap.put("hotUsers", hotUsers);
		return "/faq/questions.html";
	}

	@RequestMapping("/faq/question.jsp")
	public String question(ModelMap modelMap, Long id, Integer pageNo) {
		if (id == null) {
			modelMap.put("message", "错误的访问路径");
			return "/alert.html";
		}
		Integer pageSize = 20;
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
		Question question = questionBOImpl.getQuestionById(id);
		if (question == null) {
			modelMap.put("message", "文章不存在");
			return "/alert.html";
		}
		AccountInfo accountInfo = accountBOImpl.getAccountInfoById(question.getAccountid());
		List<Question> otherQuestions = questionBOImpl.getOther10Questions(id);
		List<Answer> answers = answerBOImpl.getPagedAnswers(id, startIndex, pageSize);
		modelMap.put("question", question);
		modelMap.put("accountInfo", accountInfo);
		modelMap.put("otherQuestions", otherQuestions);
		modelMap.put("answers", answers);
		modelMap.put("pageNo", pageNo);
		modelMap.put("pageNumArray", PageUtils.getPagedNumArray(question.getAnswers(), pageSize));
		List<AccountInfo> attentionUsers = accountBOImpl.getPagedAccountByFaqRef(id, 0, 12);
		modelMap.put("attentionUsers", attentionUsers);
		return "/faq/question.html";
	}
	
	@RequestMapping("/faq/questionFollwers.jsp")
	public String questionFollwers(ModelMap modelMap, Long id, Integer pageNo) {
		Long counts = questionBOImpl.countQuestionRefByQuestionid(id);
		if (counts == null || counts == 0) {
			return "/faq/questions.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Integer pageSize = 20;
		Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
		List<AccountInfo> attentionUsers = accountBOImpl.getPagedAccountByFaqRef(id, startIndex, pageSize);
		modelMap.put("attentionUsers", attentionUsers);
		modelMap.put("pageNo", pageNo);
		modelMap.put("pageNumArray", PageUtils.getPagedNumArray(counts, pageSize));
		List<Question> otherQuestions = questionBOImpl.getOther10Questions(id);
		modelMap.put("otherQuestions", otherQuestions);
		Question question = questionBOImpl.getQuestionById(id);
		modelMap.put("question", question);
		return "/faq/questionFollwers.html";
	}
	
	@RequestMapping("/faq/updateQuestionStatus.json")
	public ModelAndView updateQuestionStatus(ModelMap modelMap, Long questionid, Integer status) {
		ModelAndView mv = new ModelAndView();
		if (questionid == null || status == null) {
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "参数错误！");
	        return mv;
		}
		Question param = new Question();
		param.setId(questionid);
		param.setStatus(status);
		questionBOImpl.updateQuestion(param);
		mv.addObject("flag", ResultType.SUCCESS.getCode());
        mv.addObject("message", "修改成功！");
        return mv;
	}
	
	@RequestMapping("/faq/addQuestionAttention.json")
	public ModelAndView addQuestionAttention(Long questionid) {
		ModelAndView mv = new ModelAndView();
		Long accountid = ThreadObjManager.getLoginUser().getAccountid();
		if (accountid == null) {
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "请先登陆或注册！");
	        return mv;
		}
		if (questionid == null) {
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "参数错误！");
	        return mv;
		}
		Question question = questionBOImpl.getQuestionById(questionid);
		if(question==null){
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "问题不存在！");
	        return mv;
		}
		if(accountid==question.getAccountid()){
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "不能关注自己的问题！");
	        return mv;
		}
		questionBOImpl.addQuestionAttention(accountid, questionid);
		mv.addObject("flag", ResultType.SUCCESS.getCode());
        mv.addObject("message", "关注成功！");
        return mv;
	}
	
	@RequestMapping("/faq/cancelQuestionAttention.json")
	public ModelAndView cancelQuestionAttention(Long questionid) {
		ModelAndView mv = new ModelAndView();
		Long accountid = ThreadObjManager.getLoginUser().getAccountid();
		if (accountid == null) {
			mv.addObject("flag", 0);
	        mv.addObject("message", "请先登陆或注册！");
	        return mv;
		}
		if (questionid == null) {
			mv.addObject("flag", 0);
	        mv.addObject("message", "参数错误！");
	        return mv;
		}
		questionBOImpl.cancelQuestionAttention(accountid, questionid);
		mv.addObject("flag", 1);
        mv.addObject("message", "取消关注成功！");
        return mv;
	}

	@RequestMapping("/faq/answer.jsp")
	public String answer(ModelMap modelMap, Answer answer) {
		if (answer == null || answer.getQuestionid() == null) {
			modelMap.put("message", "参数错误！");
			return "/alert.html";
		}
		if (answer.getAnswer() == null || answer.getAnswer().isEmpty()) {
			modelMap.put("message", "回答内容为空！");
			return "/alert.html";
		}
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		answerBOImpl.answerQuestion(loginUser.getAccountid(), answer.getQuestionid(), answer.getAnswer());
		String redirect = "redirect:/faq/question.jsp?id=" + answer.getQuestionid();
		return redirect;
	}

	@RequestMapping("/faq/addQuestionInit.jsp")
	public String addQuestionInit(ModelMap modelMap) {
		this.initQuestionParams(modelMap);
		return "/faq/addQuestion.html";
	}

	@RequestMapping("/faq/addQuestion.jsp")
	public String addQuestion(ModelMap modelMap, Question question) {
		if (!this.validate(modelMap, question)) {
			return this.addQuestionInit(modelMap);
		}
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		question.setAccountid(loginUser.getAccountid());
		question.setQuestion(question.getQuestion().trim());
		question.setLabel(question.getLabel().trim());
		questionBOImpl.addQuestion(question);
		return "/faq/addQuestionSuccess.html";
	}

	@RequestMapping("/faq/updateQuestionInit.jsp")
	public String updateQuestionInit(ModelMap modelMap, Long id) {
		this.initQuestionParams(modelMap);
		Question question = questionBOImpl.getQuestionById(id);
		modelMap.put("question", question);
		return "/faq/updateQuestion.html";
	}

	@RequestMapping("/faq/updateQuestion.jsp")
	public String updateQuestion(ModelMap modelMap, Question question) {
		if (!this.validate(modelMap, question)) {
			return this.updateQuestionInit(modelMap, question.getId());
		}
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		question.setAccountid(loginUser.getAccountid());
		question.setQuestion(question.getQuestion().trim());
		question.setLabel(question.getLabel().trim());
		questionBOImpl.updateQuestion(question);
		return "/faq/addQuestionSuccess.html";
	}

	private void initQuestionParams(ModelMap modelMap) {
		Question Question = new Question();
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		Question.setAccountid(loginUser.getAccountid());
		Question.setStatus(QuestionStatusType.draft.getCode());
		List<Question> draftList = questionBOImpl.getQuestions(Question);
		modelMap.put("draftList", draftList);
	}

	private boolean validate(ModelMap modelMap, Question question) {
		if (question == null) {
			modelMap.put(MessageCode.key, MessageCode.PARAMS_IS_NULL);
			return false;
		}
		modelMap.put("question", question);
		if (question.getQuestion() == null || question.getQuestion().isEmpty()) {
			modelMap.put(MessageCode.key, MessageCode.FAQ_QUESTION_TITLE_NULL);
			return false;
		}
		if (question.getComplement() == null || question.getComplement().isEmpty()) {
			modelMap.put(MessageCode.key, MessageCode.FAQ_QUESTION_COMPLEMENT_NULL);
			return false;
		}
		if (question.getLabel() == null || question.getLabel().isEmpty()) {
			modelMap.put(MessageCode.key, MessageCode.FAQ_QUESTION_LABEL_NULL);
			return false;
		}
		return true;
	}
	
	@RequestMapping("/faq/setAnswer.json")
	public ModelAndView setAnswer(Long questionid, Long answerid, Boolean isAnswer) {
		ModelAndView mv = new ModelAndView();
		Long accountid = ThreadObjManager.getLoginUser().getAccountid();
		if(accountid==null){
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "请先登陆或注册！");
			return mv;
		}
		Question question = questionBOImpl.getQuestionById(questionid);
		if(question==null){
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "问题已不存在！");
			return mv;
		}
		if(question.getAccountid().compareTo(accountid)!=0){
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "这不是您的问题！");
			return mv;
		}
		answerBOImpl.setAnswerOfQuestion(questionid, answerid, isAnswer);
		mv.addObject("flag", ResultType.SUCCESS.getCode());
        mv.addObject("message", "更新成功！");
		return mv;
	}
	
	@RequestMapping("/faq/answerRef.json")
	public ModelAndView answerRef(Long questionid, Long answerid, Integer type) {
		ModelAndView mv = new ModelAndView();
		Long accountid = ThreadObjManager.getLoginUser().getAccountid();
		if(accountid==null){
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "请先登陆或注册！");
			return mv;
		}
		Question question = questionBOImpl.getQuestionById(questionid);
		if(question==null){
			mv.addObject("flag", ResultType.FAILURE.getCode());
	        mv.addObject("message", "问题已不存在！");
			return mv;
		}
		answerBOImpl.addAnswersRef(accountid, answerid, type);
		mv.addObject("flag", ResultType.SUCCESS.getCode());
        mv.addObject("message", "更新成功！");
		return mv;
	}

}
